<template>
  <view :style="themeColor">
    <view class="page">
      <view class="flex benben-position-layout flex flex-wrap align-center myProfile_flex_0"
        :style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx', }">
        <view class='flex flex-wrap align-center justify-between flex-sub myProfile_fd0_0'>
          <view class='flex flex-wrap align-center myProfile_fd0_0_c0' @tap.stop="handleJumpDiy" data-type="back"
            data-url="1">
            <text class='fu-iconfont2  myProfile_fd0_0_c0_c0'>&#xE794;</text>
          </view>
          <view class='flex flex-wrap align-stretch justify-center'>
            <text class='myProfile_fd0_0_c1_c0'>{{$t('我的资料')}}</text>
          </view>
          <view class='flex flex-wrap align-center justify-end myProfile_fd0_0_c0'>
          </view>
        </view>

      </view>
      <view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
      <!--选择生日开始 -->
      <benben-picker ref="benbenWritePickerCodepicker1" :visible.sync="pickerDiy1703520610264"
        :value.sync='dataInfo.birthday' mode='date' :picker-height='88' :current='true' :mask-show='true' fields='day'
        :timeout='true' start-year='1900-01-01' end-year='2050-12-31' from-data=''>
        <template #picker-header>
          <view class='flex flex-wrap align-center justify-between myProfile_picker1_0'>
            <text class='myProfile_picker1_0_c0' @tap="$refs.benbenWritePickerCodepicker1.cancel()">{{$t('取消')}}</text>
            <text>{{$t('选择生日')}}</text>
            <text class='myProfile_picker1_0_c2'
              @tap="$refs.benbenWritePickerCodepicker1.pickerConfirm()">{{$t('确认')}}</text>
          </view>
        </template>
      </benben-picker>
      <!--选择生日结束 -->
      <!---flex布局flex布局开始-->
      <view class="flex flex-direction flex-wrap align-stretch benben-flex-layout myProfile_flex_2">
        <view class='flex flex-wrap align-center justify-between myProfile_fd2_0'>
          <text class='myProfile_fd2_0_c0'>{{$t('头像')}}</text>
          <view class='flex flex-wrap align-center'>
            <view class="upload position-relative">

              <image class=" myProfile_fd2_0_c1_c0_image" mode="aspectFit"
                @tap="croppingUploadImage('avatar','avatar',dataInfo,dataInfo)"
                :src="dataInfo.avatar ? dataInfo.avatar : STATIC_URL+'89.png'"></image>
            </view>
            <image class='myProfile_fd2_0_c1_c1' mode="aspectFit" :src='STATIC_URL+"84.png"'></image>
          </view>
        </view>
        <!-- <view class='flex flex-wrap align-center justify-between myProfile_fd2_0'>
          <text class='myProfile_fd2_0_c0'>ID</text>
          <view class='flex flex-wrap align-center'>
            <text class='myProfile_fd2_1_c1_c0'>{{dataInfo.id}}</text>
          </view>
        </view> -->
        <view class='flex flex-wrap align-center justify-between myProfile_fd2_0' @tap.stop="handleJumpDiy"
          data-type="navigateTo" :data-url="`/pages/wode/changeNickname/changeNickname`">
          <text class='myProfile_fd2_0_c0'>{{$t('昵称')}}</text>
          <view class='flex flex-wrap align-center'>
            <input class='myProfile_fd2_2_c1_c0' type="text" :placeholder="$t('请输入昵称')" confirm-type="done"
              :maxlength="-1" :disabled='true' placeholder-style="color:#999;font-size:32rpx"
              v-model="dataInfo.nickname" />
            <image class='myProfile_fd2_0_c1_c1' mode="aspectFit" :src='STATIC_URL+"84.png"'></image>
          </view>
        </view>
        <view class='flex flex-wrap align-center justify-between myProfile_fd2_0'
          @tap.stop="pickerDiy1703520877653=true">
          <text class='myProfile_fd2_0_c0'>{{$t('性别')}}</text>
          <view class='flex flex-wrap align-center'>
            <input class='myProfile_fd2_2_c1_c0' type="text" :placeholder="$t('请选择性别')" confirm-type="done"
              :maxlength="-1" :disabled='true' placeholder-style="color:#999;font-size:32rpx" v-model="sex" />
            <image class='myProfile_fd2_0_c1_c1' mode="aspectFit" :src='STATIC_URL+"84.png"'></image>
          </view>
        </view>
        <view class='flex flex-wrap align-center justify-between myProfile_fd2_0'
          @tap.stop="pickerDiy1703520610264=true">
          <text class='myProfile_fd2_0_c0'>{{$t('生日')}}</text>
          <view class='flex flex-wrap align-center'>
            <input class='myProfile_fd2_2_c1_c0' type="text" :placeholder="$t('请选择生日')" confirm-type="done"
              :maxlength="-1" :disabled='true' placeholder-style="color:#999;font-size:32rpx"
              v-model="dataInfo.birthday" />
            <image class='myProfile_fd2_0_c1_c1' mode="aspectFit" :src='STATIC_URL+"84.png"'></image>
          </view>
        </view>
      </view>

      <!---flex布局flex布局结束-->
      <view class="flex flex-direction flex-wrap align-stretch benben-position-layout flex myProfile_flex_3">
        <view class='flex flex-wrap align-center justify-center flex-sub' @tap.stop="modifyMembershipFunc()">
          <text class='myProfile_fd3_0_c0'>{{$t('保存')}}</text>
        </view>

      </view>
      <view :style="{height: '108rpx'}"></view>
      <!--选择性别开始 -->
      <benben-picker ref="benbenWritePickerCodepicker4" :visible.sync="pickerDiy1703520877653" :label.sync='sex'
        :value.sync='dataInfo.gender' :options='sexType' mode='selector' :mask-show='true' :timeout='true'
        :picker-height='88' default-type='value' :default-props='{"label":"name","value":"value"}'>
        <template #picker-header>
          <view class='flex flex-wrap align-center justify-between myProfile_picker4_0'>
            <text class='myProfile_picker4_0_c0' @tap="$refs.benbenWritePickerCodepicker4.cancel()">{{$t('取消')}}</text>
            <text>{{$t('选择性别')}}</text>
            <text class='myProfile_picker4_0_c2'
              @tap="$refs.benbenWritePickerCodepicker4.pickerConfirm()">{{$t('确定')}}</text>
          </view>
        </template>
      </benben-picker>
      <!--选择性别结束 -->


    </view>
  </view>
</template>
<script>
  import UploadImage from '@/common/utils/upload-image.js';
  import permision from "@/js_sdk/wa-permission/permission.js"
  import {
    validate
  } from '@/common/utils/validate.js'
  export default {
    components: {},


    data() {
      return {
        "pickerDiy1703520877653": false,
        "pickerDiy1703520610264": false,
        "sexType": [{
          "name": "男",
          "value": "1",
          "image": ""
        }, {
          "name": "女",
          "value": "2",
          "image": ""
        }, {
          "name": "保密",
          "value": "0",
          "image": ""
        }],
        "sex_id": "",
        "dataInfo": {
          "invite_code": "",
          "address": "",
          "address_code": "",
          "hobby": "",
          "tags": "",
          "id": "",
          "userlevel_id": "",
          "mobile": "",
          "real_name": "",
          "avatar": "",
          "account": "",
          "email": "",
          "birthday": "",
          "gender": "",
          "create_time": "",
          "user_money": "",
          "freeze_money": "",
          "total_consumption_money": "",
          "score": "",
          "_mobile": "",
          "coupon": "",
          "collection_count": "",
          "browse_count": "",
          "total_money": "",
          "certified": "",
          "nickname": "",
          "is_pay_password": "",
          "follow_count": "",
          "is_close": ""
        },
        "change": {
          "userinfo": {
            "id": "",
            "avatar": "",
            "real_name": "",
            "nickname": "",
            "email": "",
            "gender": "",
            "mobile": "",
            "birthday": "",
            "client_id": "",
            "phone": ""
          }
        },
        "sex": "",
        isIos: true,
        isCamera: false,
        isphotoLibrary: false
      };
    },
    computed: {
      themeColor() {
        return this.$store.getters.themeColor
      },

    },
    watch: {},
    async onLoad(options) {
      this.getDataFunc()
      this.isIos = (plus.os.name == "iOS");
      if (this.isIos) {
        // 位置权限
        this.isLocation = permision.judgeIosPermission("location");

        // 相机权限
        this.isCamera = permision.judgeIosPermission("camera");

        // 存储权限
        this.isphotoLibrary = permision.judgeIosPermission("photoLibrary");

      } else {

        // 位置权限
        let result = await permision.requestAndroidPermission("android.permission.ACCESS_FINE_LOCATION");
        if (result == 1) {
          this.isLocation = true;
        } else {
          this.isLocation = false;
        }

        // 相机权限
        let result1 = await permision.requestAndroidPermission("android.permission.CAMERA");
        if (result1 == 1) {
          this.isCamera = true;
        } else {
          this.isCamera = false;
        }

        // 存储权限
        let result2 = await permision.requestAndroidPermission(
          "android.permission.READ_EXTERNAL_STORAGE"); //外部存储(含相册)读取权限
        let result3 = await permision.requestAndroidPermission(
          "android.permission.WRITE_EXTERNAL_STORAGE"); //外部存储(含相册)写入权限
        if (result2 == 1 && result3 == 1) {
          this.isphotoLibrary = true;
        } else {
          this.isphotoLibrary = false;
        }
        console.log('this.isphotoLibrary = true;', this.isCamera, this.isphotoLibrary)
      }
    },
    onUnload() {

    },
    onReady() {

    },
    onShow() {
      uni.$on('username', data => {
        console.log(data)
        this.dataInfo.nickname = data
      })
    },
    onHide() {

    },
    onResize() {

    },
    onPullDownRefresh() {

    },
    onReachBottom(e) {

    },
    onPageScroll(e) {

    },
    methods: {
      //裁剪图片上传
      croppingUploadImage(id, path, obj, obj1, fnName = false) {
        let self = this
        if (!obj) obj = this
        if (!obj1) obj1 = this
        let itemList = obj1[path] ? ['修改头像', '拍照', '从手机相册选择'] : ['拍照', '从手机相册选择']

        function imageCroppingCuccess() {
          uni.$once('image-cropping-success', (file) => {
            if (!file) return
            new UploadImage([file], {
              complete: (res) => {
                if (id) self.$set(obj, id, res[0].id)
                if (path) self.$set(obj1, path, res[0].path)
                if (fnName) self[fnName]()
              },
            })
          })
        }
        uni.showActionSheet({
          itemList: itemList,
          success: function(res) {
            if (res.tapIndex == 0 && obj1[path]) {
              uni.navigateTo({
                url: '/pages/benben-built-in/image-cropping/image-cropping?rectHeight=200&rectWidth=200&&myImgUrl=' +
                  obj1[path],
                animationDuration: 0,
              })
              imageCroppingCuccess()
            }
            if ((res.tapIndex == 1 && obj1[path]) || (res.tapIndex == 0 && !obj1[path])) {
              if (!self.isCamera) {
                self.$message.info(global.i18n.t('请前往手机设置开启权限'))
                return;
              }
              uni.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: (res) => {
                  // 跳转到图片裁切页面
                  uni.navigateTo({
                    url: `/pages/benben-built-in/image-cropping/image-cropping?rectHeight=200&rectWidth=200&fileType=${res.tempFiles[0].type}&myImgUrl=${res.tempFilePaths[0]}`,
                    animationDuration: 0,
                  })
                  imageCroppingCuccess()
                },
              })
            }
            if ((res.tapIndex == 2 && obj1[path]) || (res.tapIndex == 1 && !obj1[path])) {
              if (!self.isphotoLibrary) {
                self.$message.info(global.i18n.t('请前往手机设置开启权限'))
                return;
              }
              uni.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
                success: (res) => {
                  // 跳转到图片裁切页面
                  uni.navigateTo({
                    url: `/pages/benben-built-in/image-cropping/image-cropping?rectHeight=200&rectWidth=200&fileType=${res.tempFiles[0].type}&myImgUrl=${res.tempFilePaths[0]}`,
                    animationDuration: 0,
                  })
                  imageCroppingCuccess()
                },
              })
            }
          },
          fail: function(res) {
            console.log(res.errMsg)
          },
        })
      },
      //获取会员
      async getDataFunc() {
        //请求方法
        //数据验证

        let datadataInfo = await this.$api.get(global.apiUrls.post5c78c4772da97, {

        });

        if (datadataInfo.data.code != 1) {
          this.$message.info(datadataInfo.data.msg);
          return
        }
        let infodataInfo = datadataInfo.data;
        this.dataInfo = infodataInfo.data

        if (this.dataInfo.gender == 0) {
          this.sex = '保密';
        } else if (this.dataInfo.gender == '1') {
          this.sex = '男';
        } else if (this.dataInfo.gender == '2') {
          this.sex = '女';
        }
      },
      //修改会员信息
      async modifyMembershipFunc() {
        if (!validate(this.dataInfo.avatar, 'require')) {
          this.$message.info(global.i18n.t('用户头像不能为空'));
          return false;
        }
        if (!validate(this.dataInfo.nickname, 'require')) {
          this.$message.info(global.i18n.t('用户昵称不能为空'));
          return false;
        }
        if (!validate(this.dataInfo.gender, 'require')) {
          this.$message.info(global.i18n.t('请选择性别'));
          return false;
        }
        if (!validate(this.dataInfo.birthday, 'require')) {
          this.$message.info(global.i18n.t('请选择出生日期'));
          return false;
        }
        //请求方法
        //数据验证

        let datachange = await this.$api.post(global.apiUrls.post5cb54af125f1c, {
          avatar: this.dataInfo.avatar,
          nickname: this.dataInfo.nickname,
          gender: this.dataInfo.gender,
          birthday: this.dataInfo.birthday
        });

        if (datachange.data.code != 1) {
          this.$message.info(datachange.data.msg);
          return
        }
        let infochange = datachange.data;
        this.change = infochange.data

        this.$message.info(datachange.data.msg);
        setTimeout(() => {
          this.$urouter.navigateBack(1);
        }, 500)
      }
    }
  };
</script>
<style lang="scss" scoped>
  .page {
    width: 100vw;
    overflow-x: hidden;
    min-height: calc(100vh - var(--window-bottom));
    background: rgba(247, 243, 237, 1);
    background-size: 100% auto;
  }

  .myProfile_flex_0 {
    border-bottom: 1px solid rgba(232, 229, 216, 1);
    background: rgba(247, 243, 237, 1);
    width: 750rpx;
    height: 88rpx;
    overflow: hidden;
    z-index: 10;
    top: 0rpx;
    background-size: 100% auto !important;
  }

  .myProfile_fd0_0_c1_c0 {
    font-size: 36rpx;
    font-weight: 700;
    color: #333333;
    line-height: 50rpx;
  }

  .myProfile_fd0_0_c0_c0 {
    font-size: 36rpx;
    color: #333;
  }

  .myProfile_fd0_0_c0 {
    width: 120rpx;
  }

  .myProfile_fd0_0 {
    padding: 0rpx 32rpx 0rpx 32rpx;
  }

  .myProfile_picker1_0_c2 {
    color: rgba(145, 47, 34, 1);
  }

  .myProfile_picker1_0_c0 {
    color: rgba(191, 191, 191, 1);
  }

  .myProfile_picker1_0 {
    border-bottom: 1px solid #eee;
    background: #fff;
    padding: 0rpx 32rpx 0rpx 32rpx;
    line-height: 88rpx;
    border-radius: 25rpx 25rpx 0rpx 0rpx;
    background-size: 100% auto !important;
  }

  .myProfile_flex_2 {
    padding: 0rpx 0rpx 0rpx 32rpx;
  }

  .myProfile_fd2_2_c1_c0 {
    text-align: right;
    font-size: 32rpx;
    color: rgba(102, 102, 102, 1);
  }

  .myProfile_fd2_1_c1_c0 {
    color: #666666;
    font-size: 32rpx;
    font-weight: 400;
    line-height: 42rpx;
  }

  .myProfile_fd2_0_c1_c1 {
    width: 14rpx;
    height: 26rpx;
    border-radius: 0rpx 0rpx 0rpx 0rpx;
    margin: 0rpx 0rpx 0rpx 16rpx;
  }

  .myProfile_fd2_0_c1_c0_image {
    width: 88rpx;
    height: 88rpx;
    border-radius: 40000rpx;
  }

  .myProfile_fd2_0_c0 {
    color: #333333;
    font-size: 32rpx;
    font-weight: 500;
    line-height: 42rpx;
  }

  .myProfile_fd2_0 {
    border-bottom: 1px solid rgba(232, 229, 216, 1);
    padding: 32rpx 24rpx 32rpx 0rpx;
  }

  .myProfile_flex_3 {
    background: rgba(255, 253, 250, 1);
    width: 750rpx;
    height: 108rpx;
    overflow: hidden;
    z-index: 10;
    bottom: calc(0rpx + var(--window-bottom));
    background-size: 100% auto;
  }

  .myProfile_fd3_0_c0 {
    color: #912F22;
    font-size: 32rpx;
    font-weight: 500;
    line-height: 36rpx;
  }

  .myProfile_picker4_0_c2 {
    color: rgba(145, 47, 34, 1);
  }

  .myProfile_picker4_0_c0 {
    color: rgba(191, 191, 191, 1);
  }

  .myProfile_picker4_0 {
    border-bottom: 1px solid #eee;
    background: #fff;
    padding: 0rpx 32rpx 0rpx 32rpx;
    line-height: 88rpx;
    border-radius: 25rpx 25rpx 0rpx 0rpx;
    background-size: 100% auto !important;
  }
</style>
